package com.sino.androiddesign.test;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.sino.androiddesign.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gyq on 2017/3/27 15:44
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.SimpleHolder> implements SlidingButtonView.IonSlidingButtonListener {
    private Context mContext;
    private List<String> mDatas = new ArrayList<>();

    private IonSlidingViewClickListener mIDeleteBtnClickListener;

    private SlidingButtonView mMenu;

    public RecyclerAdapter(Context context) {
        mContext = context;

        for (int i = 0; i < 10; i++) {
            mDatas.add(i+".世界最可爱的小狗");
        }
    }


    @Override
    public SimpleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        /*if (mContext == null) {
            mContext = parent.getContext();
        }*/
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_item, parent, false);
        return new SimpleHolder(view);
    }

    @Override
    public void onBindViewHolder(final SimpleHolder holder, int position) {
        holder.mText.setText(mDatas.get(position));
        WindowManager systemService = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        systemService.getDefaultDisplay().getMetrics(displayMetrics);
        holder.layout_content.getLayoutParams().width = displayMetrics.widthPixels;

        holder.mText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext,",,,,....",Toast.LENGTH_SHORT).show();
                //判断是否有删除菜单打开
                if (menuIsOpen()) {
                    closeMenu();//关闭菜单
                } else {
                    int n = holder.getLayoutPosition();
                    mIDeleteBtnClickListener.onItemClick(view, n);
                }
            }
        });

        holder.tv_Delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext,",,,,....ddddd",Toast.LENGTH_SHORT).show();
                int n = holder.getLayoutPosition();

                mIDeleteBtnClickListener.onDeleteBtnCilck(view,n);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class SimpleHolder extends  RecyclerView.ViewHolder {
        public TextView tv_Delete;
        public TextView mText;
        public LinearLayout layout_content;
        public SimpleHolder(View itemView) {
            super(itemView);

            tv_Delete = (TextView)itemView.findViewById(R.id.tv_delete);

            mText = (TextView)itemView.findViewById(R.id.text);

            layout_content = (LinearLayout)itemView.findViewById(R.id.layout_content);

            ((SlidingButtonView)itemView).setSlidingButtonListener(RecyclerAdapter.this);
        }


    }

    public void addData(int position) {
        mDatas.add(position, "添加项");
        notifyItemInserted(position);
    }

    public void removeData(int position){
        mDatas.remove(position);
        notifyDataSetChanged();

    }

    @Override
    public void onMenuIsOpen(View view) {
        mMenu = (SlidingButtonView) view;
    }

    @Override
    public void onDownOrMove(SlidingButtonView slidingDeleteView) {
        if(menuIsOpen()){
            if(mMenu != slidingDeleteView){
                closeMenu();
            }
        }
    }

    /**
     * 关闭菜单
     */
    public void closeMenu() {
        mMenu.closeMenu();
        mMenu = null;

    }
    /**
     * 判断是否有菜单打开
     */
    public Boolean menuIsOpen() {
        if(mMenu != null){
            return true;
        }
        return false;
    }

    public void setOnSlidListener(IonSlidingViewClickListener listener) {
        mIDeleteBtnClickListener = listener;
    }

    public interface IonSlidingViewClickListener {
        void onItemClick(View view,int position);
        void onDeleteBtnCilck(View view,int position);
    }
}